import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:ljy_p_app/diffwidget/diff_scale_text.dart';
import 'package:ljy_p_app/diffwidget/mole_widget.dart';
import 'package:ljy_p_app/launch/controller/lj_launch_controller.dart';
import 'package:ljy_p_app/routes/lj_routes.dart';
import 'package:ljy_p_app/style/lj_style.dart';
import 'package:rive/rive.dart' as rive;

class LjWelcome extends StatefulWidget {
  @override
  State<LjWelcome> createState() => _LjWelcomeState();
}

class _LjWelcomeState extends State<LjWelcome> {
  LjLaunchController controller = Get.put(LjLaunchController());

  bool hadInit = false;

  final text = "".obs;

  final fontsize = 76.0.obs;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (hadInit) {
      return;
    }
    hadInit = true;

    /// 500毫秒之后
    Future.delayed(Duration(milliseconds: 500), () {
      text.value = "Welcome";
      fontsize.value = 60.0;
    });
    /// 1s + 500毫秒之后
    Future.delayed(Duration(seconds: 1, milliseconds: 500), () {
      text.value = "LJYGithubApp";
      fontsize.value = 60.0;
    });
    /// 3s + 500毫秒之后
    Future.delayed(Duration(seconds: 3, milliseconds: 500), () {
      if (controller.isLogin.value) {
        /// 已登录
        Get.offAllNamed(LjRoutes.home);
      } else {
        /// 未登录
        Get.offAllNamed(LjRoutes.login);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    double size = 200;
    return Material(
      child: Container(
        color: LJColors.white,
        child: Stack(
          children: [
            Center(
              child: Image(image: AssetImage('assets/images/welcome.png'), width: double.infinity,),
            ),
            Align(
              // x 和 y 的范围是从 -1.0 到 1.0。-1.0 表示左/上对齐，1.0 表示右/下对齐，0.0 表示居中对齐
              alignment: Alignment(0.0, 0.3),
              child: Obx(() => DiffScaleText(
                text: text.value,
                textStyle: GoogleFonts.akronim().copyWith(
                  fontSize: fontsize.value,
                  color: LJColors.primaryDarkValue,
                ),
              )),
            ),
            Align(
              alignment: Alignment(0.0, 0.8),
              child: Mole(),
            ),
            Align(
              alignment: Alignment(0.0, 0.9),
              child: SizedBox(
                width: size,
                height: size,
                child: rive.RiveAnimation.asset(
                  'assets/file/launch.riv',
                  animations: ['lookUp'],
                  onInit: (arb) {
                    var controller = rive.StateMachineController.fromArtboard(arb, "birb");
                    var smi = controller?.findInput<bool>("dance");
                    arb.addController(controller!);
                    smi?.value == true;
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}